package com.oweight.sagacious.payment.infrastructure.configuration;

import com.oweight.sagacious.auth.client.interceptor.ServiceAuthRestInterceptor;
import com.oweight.sagacious.auth.client.interceptor.UserAuthRestInterceptor;
import com.oweight.sagacious.common.handler.GlobalExceptionHandler;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.util.ArrayList;
import java.util.Collections;

/**
 * WebConfig
 *
 * @author 周广明
 * @version v1 2019/10/21 16:13
 */
@Slf4j
@Configuration("adminWebConfig")
@Primary
public class WebConfiguration implements WebMvcConfigurer {
    
    @Bean
    GlobalExceptionHandler getGlobalExceptionHandler() {
        return new GlobalExceptionHandler();
    }

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        log.info("加载拦截配置...");
        // 服务鉴权拦截器
        registry.addInterceptor(getServiceAuthRestInterceptor())
                .addPathPatterns(getIncludePathPatterns())
                .excludePathPatterns(getExcludePathPatterns());

        // 用户鉴权拦截器
        registry.addInterceptor(getUserAuthRestInterceptor())
                .addPathPatterns(getIncludePathPatterns())
                .excludePathPatterns(getExcludePathPatterns());
    }

    @Bean
    ServiceAuthRestInterceptor getServiceAuthRestInterceptor() {
        return new ServiceAuthRestInterceptor();
    }

    @Bean
    UserAuthRestInterceptor getUserAuthRestInterceptor() {
        return new UserAuthRestInterceptor();
    }

    /**
     * 需要用户和服务认证判断的路径
     */
    private ArrayList<String> getIncludePathPatterns() {
        ArrayList<String> list = new ArrayList<>();
        String[] urls = {
                "/**"
        };
        Collections.addAll(list, urls);
        return list;
    }

    /**
     * 不需要用户和服务认证判断的路径
     */
    private ArrayList<String> getExcludePathPatterns() {
        ArrayList<String> list = new ArrayList<>();
        String[] urls = {
                //"/**"
        };
        Collections.addAll(list, urls);
        return list;
    }

}
